﻿Imports System.Data.SqlClient
Public Class members_productupdate
    Inherits System.Web.UI.Page
    Private sSQL As String
    Private objrd As SqlDataReader = Nothing
    Private objfind As New GetData
    Private ps As New PreparedStatement(sSQL)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("uid") <> Nothing And Session("uid") <> "") Then
            Dim productName, priceUnit, specification, brand, location, productCategory, productSubCategory, description As String
            Dim productId, fileFullName, fileName, photoPath As String
            Dim productType As Boolean
            Dim price
            Dim strErr As String
            strErr = ""
            If (Request("submitproduct") <> "" And Request("submitproduct") <> Nothing) Then
                If (Request("c_product_name") = "" Or Request("c_product_name") = Nothing) Then
                    Me.c_product_name_label.Text = "请输入产品名称！"
                    Exit Sub
                Else
                    Me.c_product_name_label.Text = ""
                End If
                If (Request("c_product_category") = "" Or Request("c_product_category") = Nothing) Then
                    Me.c_category_label.Text = "请输入产品分类！"
                    Exit Sub
                Else
                    Me.c_category_label.Text = ""
                End If
                If (Request("c_description") = "" Or Request("c_description") = Nothing) Then
                    Me.c_description_label.Text = "请输入详细描述！"
                    Exit Sub
                Else
                    Me.c_description_label.Text = ""
                End If
                productId = Request("productId")
                If (Request("c_productType") = "1") Then
                    productType = True
                ElseIf (Request("c_productType") = "0") Then
                    productType = False
                End If
                productName = Request("c_product_name")
                If (Request("c_price") <> Nothing And Request("c_price") <> "") Then
                    price = Request("c_price")
                Else
                    price = "0"
                End If
                priceUnit = Request("c_price_unit")
                specification = Request("c_specification")
                brand = Request("c_brand")
                location = Request("c_location")
                productCategory = Request("c_product_category")
                If (Request("c_product_subcategory") <> Nothing And Request("c_product_subcategory") <> "") Then
                    productSubCategory = Request("c_product_subcategory")
                Else
                    productSubCategory = ""
                End If
                description = Request("c_description")
                ps.strSql = "update product_info set product_name=@productName,product_type=@productType,price=@price," & _
                    "price_unit=@priceUnit,specification=@specification,brand=@brand,location=@location,category_id=@category," & _
                    "sub_category_id=@subCategory,description=@description where product_id=@productId"
                ps.parameters.Clear()
                ps.addParameter("productId", productId)
                ps.addParameter("productType", productType)
                ps.addParameter("productName", productName)
                ps.addParameter("price", price)
                ps.addParameter("priceUnit", priceUnit)
                ps.addParameter("specification", specification)
                ps.addParameter("brand", brand)
                ps.addParameter("location", location)
                ps.addParameter("category", productCategory)
                ps.addParameter("subCategory", productSubCategory)
                ps.addParameter("description", description)
                objfind.es(ps, strErr)

                If (image0.PostedFile.FileName <> Nothing) Then
                    fileFullName = image0.PostedFile.FileName
                    fileName = fileFullName.Substring(fileFullName.LastIndexOf("\\") + 1)
                    System.IO.Directory.CreateDirectory(Server.MapPath("../../") & "pic/product/" & productId)
                    image0.PostedFile.SaveAs(Server.MapPath("../../") & "pic/product/" & productId & "/" & fileName)
                    photoPath = "/pic/product/" & productId & "/" & fileName
                    ps.strSql = "update product_info set photo_path = @photoPath where product_id=@productId"
                    ps.parameters.Clear()
                    ps.addParameter("photoPath", photoPath)
                    ps.addParameter("productId", productId)
                    objfind.es(ps, strErr)
                    Me.id0.Value = productId
                    imageDiv0.InnerHtml = "<img src=""" & photoPath & """ width=""100"" height=""100"" />"
                End If

                Response.Redirect("members-supplyManagement.aspx")
            ElseIf Request("c_product_category") = Nothing Or Request("c_product_category") = "" Then
                productId = Request("productId")
                ps.strSql = "select * from product_info where product_id=@productId"
                ps.parameters.Clear()
                ps.addParameter("productId", productId)
                Dim conn As SqlConnection = objfind.getConn()

                objrd = objfind.dr(ps, strErr, conn, objrd)
                If (objrd.Read()) Then
                    Me.productId.Value = productId
                    Me.recommendProducts.HRef = "members-productupdatepic.aspx?productId=" & productId
                    If (objrd("product_type").ToString.ToLower = "false") Then
                        Me.c_cproductType.Checked = True
                    ElseIf (objrd("product_type").ToString.ToLower = "true") Then
                        Me.c_fproductType.Checked = True
                    End If
                    Me.c_product_name.Text = objrd("product_name").ToString
                    Me.c_price.Text = objrd("price").ToString
                    Me.c_price_unit.SelectedValue = objrd("price_unit").ToString
                    Me.c_specification.Text = objrd("specification").ToString
                    Me.c_brand.Text = objrd("brand").ToString
                    Me.c_location.Text = objrd("location").ToString
                    Me.c_description.Text = objrd("description").ToString
                    If (objrd.Item("photo_path").ToString.Trim <> "" And objrd.Item("photo_path") <> Nothing) Then
                        imageDiv0.InnerHtml = "<img src=""" & objrd.Item("photo_path") & """ width=""100"" height=""100"" />"
                    End If
                    id0.Value = objrd.Item("product_id").ToString

                    productCategory = objrd("category_id").ToString
                    productSubCategory = objrd("sub_category_id").ToString
                    ps.strSql = "select * from category where lang_flag='zh'"
                    ps.parameters.Clear()
                    objrd = objfind.dr(ps, strErr, conn, objrd)
                    c_product_category.Items.Clear()
                    c_product_category.Items.Add(New ListItem("请选择一级分类", ""))
                    While (objrd.Read())
                        c_product_category.Items.Add(New ListItem(
                        objrd("Category_Name").ToString(), objrd("Category_ID").ToString()))
                    End While
                    c_product_category.SelectedValue = productCategory
                    ps.strSql = "select * from sub_category where category_id=@categoryId and lang_flag='zh'"
                    ps.parameters.Clear()
                    ps.addParameter("categoryId", productCategory)
                    objrd = objfind.dr(ps, strErr, conn, objrd)
                    c_product_subcategory.Items.Clear()
                    c_product_subcategory.Items.Add(New ListItem("请选择二级分类", ""))
                    While (objrd.Read)
                        c_product_subcategory.Items.Add(New ListItem(
                            objrd("Sub_Category_Name").ToString(), objrd("Sub_Category_ID").ToString()))
                    End While
                    c_product_subcategory.SelectedValue = productSubCategory
                End If
                objfind.closeConn(conn)

            End If
        Else
            Response.Redirect("../cn_login.aspx")
        End If
    End Sub

    Protected Sub c_product_category_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles c_product_category.SelectedIndexChanged
        Dim categoryId, strErr As String
        strErr = ""
        categoryId = c_product_category.SelectedValue
        ps.strSql = "select * from sub_category where category_id=@categoryId and lang_flag='zh'"
        ps.parameters.Clear()
        ps.addParameter("categoryId", categoryId)
        Dim conn As SqlConnection = objfind.getConn()

        objrd = objfind.dr(ps, strErr, conn, objrd)
        c_product_subcategory.Items.Clear()
        c_product_subcategory.Items.Add(New ListItem("请选择二级分类", ""))
        While (objrd.Read())
            c_product_subcategory.Items.Add(New ListItem(
            objrd("Sub_Category_Name").ToString(), objrd("Sub_Category_ID").ToString()))
        End While
        c_product_category.Items.Clear()
        c_product_category.Items.Add(New ListItem("请选择一级分类", ""))
        ps.strSql = "select * from category where lang_flag='zh'"
        ps.parameters.Clear()
        objrd = objfind.dr(ps, strErr, conn, objrd)
        While (objrd.Read())
            c_product_category.Items.Add(New ListItem(
            objrd("Category_Name").ToString(), objrd("Category_ID").ToString()))
        End While
        objfind.closeConn(conn)

        c_product_category.SelectedValue = categoryId
    End Sub

    Protected Sub deleteImage0_Click(ByVal sender As Object, ByVal e As EventArgs) Handles deleteImage0.Click
        Dim productId, strErr, id, path As String
        strErr = ""
        id = id0.Value
        productId = Me.productId.Value
        If (id <> Nothing And id <> "") Then
            ps.strSql = "select * from product_info where product_id = @productId"
            ps.parameters.Clear()
            ps.addParameter("productId", productId)
            Dim conn As SqlConnection = objfind.getConn()

            objrd = objfind.dr(ps, strErr, conn, objrd)
            If (objrd.Read()) Then
                path = objrd.Item("photo_path")
                'If (path.Trim <> "" And path <> Nothing) Then
                'path = path.Substring(6)
                'End If
                System.IO.File.Delete(Server.MapPath("../../") & path)
                imageDiv0.InnerHtml = "<input type=""file"" name=""image0"" id=""image0"" runat=""server""/>"
                Me.id0.Value = ""
                ps.strSql = "update product_info set photo_path='' where product_id = @productId"
                ps.parameters.Clear()
                ps.addParameter("productId", productId)
                objfind.es(ps, strErr)
                'Response.Redirect("members-productupdate.aspx?productId=" & productId)
            End If
            objfind.closeConn(conn)

        End If
    End Sub
End Class